package com.example.wupic.bestball;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

public class RegisterActivity extends AppCompatActivity {
    long day = 24 * 60 * 60 * 1000;
    EditText edit_account = null;
    EditText edit_password = null;
    EditText edit_phone = null;
    EditText edit_birthday = null;
    EditText edit_address = null;
    RadioGroup radiogroup_sex = null;
    RadioButton radio_male = null;
    RadioButton radio_famale = null;

    Button btn_submit=null;

    Calendar ca = Calendar.getInstance();
    int  mYear = ca.get(Calendar.YEAR);
    int  mMonth = ca.get(Calendar.MONTH);
    int  mDay = ca.get(Calendar.DAY_OF_MONTH);

    Activity that=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        edit_account = findViewById(R.id.edit_account);
        edit_password = findViewById(R.id.edit_password);
        edit_phone = findViewById(R.id.edit_phone);
        edit_birthday = findViewById(R.id.edit_birthday);
        edit_address = findViewById(R.id.edit_address);
        radiogroup_sex = findViewById(R.id.radio_group_sex);
        radio_famale = findViewById(R.id.radio_famale);
        radio_male = findViewById(R.id.radio_male);
        btn_submit= findViewById(R.id.btn_submit);

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //注册数据保存
                //1.获取数据
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        insertuser();
                    }
                }).start();

                //提示

            }
        });

        edit_birthday.setInputType(InputType.TYPE_NULL);//禁止输入法
         edit_birthday.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //打开日期选择器
                 // showDatePickDialog();

                 new DatePickerDialog(RegisterActivity.this, onDateSetListener, mYear, mMonth, mDay).show();
             }
         });

    }
    public  void  insertuser(){
        String account=   edit_account.getText().toString();//拿到用户填写的数据
        String password=  edit_password.getText().toString();//拿到用户填写的数据
        String phone=     edit_phone.getText().toString();//拿到用户填写的数据
        String birthday=  edit_birthday.getText().toString();//拿到用户填写的数据
        String address=   edit_address.getText().toString();//拿到用户填写的数据
        String sex="";
        if(radio_male.isChecked()){
            sex="男";
        }else{
            sex="女";
        }
        //2.数据插入数据库：
        Connection connection=null;
        try {
//            connection= DBManager.getConnection();
            PreparedStatement pre= connection.prepareStatement("insert into t_user(t_user_account,t_user_password," +
                    " t_user_phone,t_user_address,t_user_sex,t_user_birthday)" +
                    " values(?,?,?,?,?,?)");
            //给sql语句设置参数值
            pre.setObject(1,account);
            pre.setObject(2,password);
            pre.setObject(3,phone);
            pre.setObject(4,address);
            pre.setObject(5,sex);
            pre.setObject(6,birthday);
            pre.execute();
            pre.close();
            Looper.prepare();
            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
            that.finish();
            Looper.loop();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            days=year+"-"+(mMonth+1)+"-"+mDay;
            edit_birthday.setText(days);
        }
    };
}

